package com.tr.rentcar.controller.userc;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.DateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tr.rentcar.common.StringUtils;
import com.tr.rentcar.model.bean.CarComment;
import com.tr.rentcar.model.bean.Order;
import com.tr.rentcar.model.bean.User;
import com.tr.rentcar.model.bean.UserRecord;
import com.tr.rentcar.model.dao.CarCommentDao;
import com.tr.rentcar.model.dao.OrderDao;
import com.tr.rentcar.model.dao.UserDao;
import com.tr.rentcar.model.dao.UserJfDao;
import com.tr.rentcar.model.dao.UserRecordDao;

/**
 * Servlet implementation class OrderChuli
 */
@WebServlet("/user/orderhandle")
public class UserOrderHandleController extends HttpServlet {
	OrderDao od=new OrderDao();
	UserRecordDao ur=new UserRecordDao();
	UserDao u=new UserDao();
	CarCommentDao cc=new CarCommentDao();
	UserJfDao ujf=new UserJfDao();
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int oid=Integer.parseInt(request.getParameter("oid")) ;
		String ono=request.getParameter("ono");
		Double omoney=Double.valueOf(request.getParameter("omoney"));
		User user=(User) request.getSession().getAttribute("userinfo");
		try {
			User userd=u.findByid(user.getUid());
			Double shengyv=userd.getUmoney()-omoney;
			Double sheng=userd.getUmoney()+omoney;
			Order order=od.findByOid(oid);
			PrintWriter out=response.getWriter();
			if (!StringUtils.isEmpty(ono)) {
					u.xgmoney(shengyv,user.getUid());
					od.updatestatus("未处理",oid);
					UserRecord urr=new UserRecord();
					urr.setuId(user.getUid());
					urr.setUrWay("扣费");
					urr.setUrMoney(omoney);
					urr.setUrName(userd.getUname());
					urr.setUrdoWay("用户");
					ur.save(urr);
					user.setUmoney(shengyv);
					request.getSession().setAttribute("userinfo", user);
					out.println("付款成功");
			}else {
					if (order.getoStatus().equals("未处理")) {
						u.xgmoney(sheng,user.getUid());
						od.updatestatus("已取消",oid);
						UserRecord urr=new UserRecord();
						urr.setuId(user.getUid());
						urr.setUrWay("退款");
						urr.setUrMoney(omoney);
						urr.setUrName(userd.getUname());
						urr.setUrdoWay("用户");
						ur.save(urr);
						user.setUmoney(sheng);
						request.getSession().setAttribute("userinfo", user);
						out.println("退单成功");
					}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int cid=Integer.parseInt(request.getParameter("cid")) ;
		int uid=Integer.parseInt(request.getParameter("uid"));
		int oid=Integer.parseInt(request.getParameter("oid"));
		String lei=request.getParameter("lei");
		PrintWriter out=response.getWriter();
		try {
			CarComment comment=cc.findByoid(oid);
			if (comment!=null) {
				Date date=new Date();
				String s = DateFormat.getDateInstance().format(date);
				lei=comment.getCcContent()+"/追加评价("+s+")："+lei;
				if (cc.updateContent(lei,oid)) {
					out.println("追加评价成功");
				}
				return;
			}
			if (cc.save(lei, uid, cid, oid)) {
				if (lei.length()>=15) {
					u.xiugai(5, uid);
					ujf.pingjia(5, "评价订单",uid );
					out.println("恭喜您评价成功获得5积分，在积分详细中查看");
					return;
				}
				out.println("评价成功");
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
